package com.imooc.newretail.bpl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imooc.newretail.common.exceptions.BplException;
import com.imooc.newretail.bpl.entity.Tenant;
import com.imooc.newretail.bpl.entity.TenantUser;
import com.imooc.newretail.bpl.enums.ErrCodeEnum;
import com.imooc.newretail.bpl.enums.TenantStatusEnum;
import com.imooc.newretail.bpl.mapper.TenantMapper;
import com.imooc.newretail.bpl.service.IDeptService;
import com.imooc.newretail.bpl.service.ITenantService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imooc.newretail.bpl.service.ITenantUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 租户信息表 服务实现类
 * </p>
 *
 * @author Hank
 * @since 2020-06-27
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    IDeptService deptService;
    @Autowired
    ITenantUserService tenantUserService;

    @Override
    public boolean disableFranchisee(String tenantId, String remark) {
        return updateStatus(tenantId, TenantStatusEnum.DISABLE, remark);
    }

    @Override
    public boolean enableFranchisee(String tenantId) {
        return updateStatus(tenantId, TenantStatusEnum.ENABLE, "");
    }

    @Override
    @Transactional
    public boolean removeTenantAndUser(String tenantId) throws BplException {
        QueryWrapper query = new QueryWrapper();
        query.eq("tenant_id", tenantId);
        deptService.remove(query);
        tenantUserService.remove(query);
        baseMapper.deleteById(tenantId);
        return true;
    }

    /**
     * 修改租户状态
     *
     * @param tenantId
     * @param remark
     * @return
     * @throws BplException
     */
    private boolean updateStatus(String tenantId, TenantStatusEnum status, String remark) throws BplException {
        Tenant tenant = baseMapper.selectById(tenantId);
        if (tenant == null) {
            throw new BplException(ErrCodeEnum.NO_DATA);
        }
        tenant.setStatus(status.getCode());
        if (StringUtils.isNotEmpty(remark))
            tenant.setRemark(StringUtils.isEmpty(tenant.getRemark()) ? remark : tenant.getRemark() + ">" + remark);
        tenant.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(tenant) > 0 ? true : false;
    }

    /***
     * 添加用户到指定的租户部门
     * @param user
     * @return
     */
    @Override
    public boolean addUser(TenantUser user) throws BplException {
        if (StringUtils.isEmpty(user.getTenantId())
                || StringUtils.isEmpty(user.getUserId())) {
            throw new BplException(ErrCodeEnum.PARAM_IS_EMPTY);
        }
        Tenant tenant = baseMapper.selectById(user.getTenantId());
        if (tenant == null) {
            throw new BplException(ErrCodeEnum.NO_DATA);
        }
        return tenantUserService.save(user);
    }

    /***
     * 根据部门ID获取用户列表
     * @param tenantId
     * @return
     */
    public List<TenantUser> getUsersByTenantId(String tenantId) {
        QueryWrapper query = new QueryWrapper();
        query.eq("tenant_id", tenantId);
        return tenantUserService.list(query);
    }

    public boolean removeUser(String tenantId, String userId) {
        QueryWrapper query = new QueryWrapper();
        query.eq("tenant_id", tenantId);
        query.eq("user_id", userId);
        return tenantUserService.remove(query);
    }


}
